using System;
using System.Text.RegularExpressions;
using SharedObjects;
using TheGame.Server;
using TheGameLib;

namespace TheGame.Webservices
{
	public partial class TheGameService
	{
		void InstanceGlobalChatMessageReceived(object sender, ChatEventArgs e)
		{
			if (Callback != null)
				Callback.GlobalChatMessage(e.ChatMessage);
		}

		public void SendGlobalChatMessage(Guid sessionId, ChatMessage message)
		{
			Agent me;
			if (!CheckIsValidAgent(sessionId, out me, true))
			{
				SendServerError(ServerErrorSeverity.Error, "Invalid session id.");
				return;
			}
			message.Timestamp = DateTime.Now;
			message.Sender = me.DisplayName;
			World.Instance.SendGlobalChatMessage(message);
		}

		void ConnectionChatMessageReceived(object sender, ChatEventArgs e)
		{
			if (Callback != null)
				Callback.LocalChatMessage(e.ChatMessage);
		}

		public void SendChatMessage(Guid sessionId, ChatMessage message)
		{
			Agent me;
			if (!CheckIsValidAgent(sessionId, out me))
			{
				SendServerError(ServerErrorSeverity.Error, "Invalid session id.");
				return;
			}
			AgentSessionObject aso = World.Instance.GetAgentSession(sessionId);
			if (aso == null)
			{
				SendServerError(ServerErrorSeverity.Fatal, "No session object found for session id.");
				return;
			}
			AgentConnection conn;
			lock (aso)
			{
				conn = aso.GetAgentConnectionByTargetSystem(message.SystemId);
			}
			if (conn == null)
			{
				SendServerError(ServerErrorSeverity.Error, "No connection exists to target system.");
				return;
			}
			Regex messageCommandParser = new Regex(@"\A(?<cmd>[/]+)(?<command>[nick|msg|report]+) (?<parameter>\S+)(?<rest>.*)");
			Match m = messageCommandParser.Match(message.Message);
			if (m.Success)
			{
				string command = m.Groups["command"].Value;
				switch (command)
				{
					case "nick":
						string newnick = m.Groups["parameter"].Value;
						if (World.Instance.IsChatNameLegit(newnick, message.SystemId))
						{
							conn.ChatNick = newnick;
							conn.SendChatMessage(new ChatMessage { Message = "Changed nick to " + newnick, Sender = "Chat Server", SystemId = conn.TargetSystemId, Timestamp = DateTime.Now });
						}
						else
							conn.SendChatMessage(new ChatMessage { Message = "Cannot change to this nick. Its already in use.", Sender = "Chat Server", SystemId = conn.TargetSystemId, Timestamp = DateTime.Now });
						break;
					case "msg":
						string targetuser = m.Groups["parameter"].Value.ToLower();
						//send private message to user
						message.Timestamp = DateTime.Now;
						message.Sender = string.IsNullOrEmpty(conn.ChatNick) ? me.DisplayName : conn.ChatNick;
						message.Message = m.Groups["rest"].Value;
						if (!World.Instance.SendPrivateChatMessage(targetuser, message))
							conn.SendChatMessage(new ChatMessage { Message = "Failed to send private message to " + targetuser, Sender = "Chat Server", SystemId = conn.TargetSystemId, Timestamp = DateTime.Now });
						break;
					//future reporting command (report user to admin for disciplinary measures, currently unused)
					//case "report":
					//    string reportedChatter = m.Groups["parameter"].Value;
					//    string reportReason = m.Groups["rest"].Value;
					//    break;
				}
			}
			else
			{
				message.Timestamp = DateTime.Now;
				message.Sender = string.IsNullOrEmpty(conn.ChatNick) ? me.DisplayName : conn.ChatNick;
				World.Instance.SendLocalChatMessage(message);
			}
		}
	}
}